function alertMsg(txt) {
    var alertFram = document.createElement("DIV");
    alertFram.id = "alertFram";
    alertFram.style.position = "fixed";
    alertFram.style.width = "100%";
    alertFram.style.textAlign = "center";
    alertFram.style.top = "40%";
    alertFram.style.zIndex = "10001";
    strHtml = " <span style=\"font-family: 微软雅黑;display:inline-block;background:#333;color:#fff;padding:0 20px;line-height:36px;border-radius:6px; \">" + txt + "</span>";
    alertFram.innerHTML = strHtml;
    document.body.appendChild(alertFram);
    setTimeout((function () {
        alertFram.style.display = "none";
    }), 2000);
}

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
    return (false);
}

function photoCompress(file, w, objDiv) {
    var ready = new FileReader();
    /*开始读取指定的Blob对象或File对象中的内容. 当读取操作完成时,readyState属性的值会成为DONE,如果设置了onloadend事件处理程序,则调用之.同时,result属性中将包含一个data: URL格式的字符串以表示所读取文件的内容.*/
    ready.readAsDataURL(file);
    ready.onload = function () {
        var re = this.result;
        canvasDataURL(re, w, objDiv)
    }
}

function canvasDataURL(path, obj, callback) {
    var img = new Image();
    img.src = path;
    img.onload = function () {
        var that = this;
        // 默认按比例压缩
        var w = (that.width)/2,
            h = (that.height)/2,
            scale = w / h;
        w = obj.width || w;
        h = obj.height || (w / scale);
        var quality = 0.7;  // 默认图片质量为0.7
        //生成canvas
        var canvas = document.createElement('canvas');
        var ctx = canvas.getContext('2d');
        // 创建属性节点
        var anw = document.createAttribute("width");
        anw.nodeValue = w;
        var anh = document.createAttribute("height");
        anh.nodeValue = h;
        canvas.setAttributeNode(anw);
        canvas.setAttributeNode(anh);
        ctx.drawImage(that, 0, 0, w, h);
        // 图像质量
        if (obj.quality && obj.quality <= 1 && obj.quality > 0) {
            quality = obj.quality;
        }
        // quality值越小，所绘制出的图像越模糊
        var base64 = canvas.toDataURL('image/jpeg', quality);
        // 回调函数返回base64的值

        callback(base64);
    }
}

/**
 * 将以base64的图片url数据转换为Blob
 * @param urlData
 *            用url方式表示的base64图片数据
 */
function convertBase64UrlToBlob(urlData) {
    var arr = urlData.split(','), mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], {type: mime});
}


var xhr;

//上传文件方法
function UpladFile(event, fn) {

	var fileObj = event
 
    var form = new FormData(); // FormData 对象

    photoCompress(fileObj, {
        quality: 0.2
    }, function (base64Codes) {
		fn(base64Codes)
    });

}

//上传成功响应
function uploadComplete(evt) {
    //服务断接收完文件返回的结果

    var data = JSON.parse(evt.target.responseText);
    if (data.success) {
        alert("上传成功！");
    } else {
        alert("上传失败！");
    }

}

//上传失败
function uploadFailed(evt) {
    alert("上传失败！");
}

//取消上传
function cancleUploadFile() {
    xhr.abort();
}

// 统计数据
function uploadBehaviour(channel_no,user_id , mobile , car_type){
    var cn = localStorage._cn != null && localStorage._cn != "" && localStorage._cn != undefined ? localStorage._cn : "";
    $.ajax({
        type : "GET",  //提交方式
        url : "/activity/default",//路径
        data : {
            channel_no: channel_no + cn,
            user_id:user_id ? user_id : "",
            mobile:mobile ? mobile : "",
            car_type:car_type ? car_type : ""
        },
        dataType: "json",
        success : function(response) {//返回数据根据结果进行相应的处理
            console.log(response)
        }
    });
}
// 